var rootURL = "";
var page = "";
// retriveTapestryPageName();
loadPage();

function loadPage(){
// jQuery(document).ready(function(){
// jQuery("#bottom").load("bottom.html");
// });
}
function resultOnLoad(part){
	url=window.location.href;
	if(url.charAt(url.length-1)=="m"){
		window.location.href=url+"/"+part;
		return;
	}
	
	// sendAjaxRequest(new Array("search","${part}"));
}

function searchPart(keyword) {
	if (keyword == "" || keyword == null || keyword == "null"||keyword.charAt(0)=="点") {
		return;
	}
	// window.location.href = rootURL + "ConnectorItem?k=" + keyword;
	window.location.href = rootURL + keyword+".html";
}

function retriveTapestryPageName() {
	var href = window.location.href;
	var splits = href.split("\/");
	for (i = 0; i < 4; i++) {
		rootURL += splits[i] + "/";
	}
	page = splits[splits.length - 1];
	splitPage = page.split("?");
	if (splitPage.length == 2) {
		page = splitPage[0];
	}
}

function sendAjaxRequest(paraArray) {
	// alert(paraArray[0]+" "+paraArray[1]);
	if (paraArray == null && paraArray.length == 0) {
		return;
	}
	type = paraArray[0];
	var url = "AjaxServer";
	var param = "type=" + type + "&requestPage=" + page;
	if (type == "weather") {
		param += "&url=" + paraArray[1] + "&city=" + paraArray[2];
		callBack = showWeather;
	}else if(type=="search"){
		param += "&k="+ paraArray[1];
		callBack = ajaxSearch;
	}else if (type == "weather2") {
		param += "&city="+ paraArray[1];
		callBack = showWeather2;
	}else if(type=="publishSelect"){
		param +="&t="+paraArray[1];
		callBack=updateSub;
	}
	// alert("href:" + window.location.href + " root:" + rootURL + " page:" +
	// page);
	var myAjax = new Ajax.Request(url, {
		method :'post',
		parameters :param,
		onComplete :callBack
	});

}

function ajaxSearch(originalRequest){
	searchPart(originalRequest.responseText);
}

function showWeather(originalRequest) {
	if(originalRequest==""||originalRequest==undefined||originalRequest==null){
		return;
	}
	var splits = originalRequest.responseText.split(",");
	$("weather").innerHTML = splits[1] + " " + splits[0];
}

function showWeather2(originalRequest) {
	$("weather").innerHTML = originalRequest.responseText;
}

function showHideLoginDiv(divId, topPx) {
	var top = $(divId).style.top;
	px = topPx;
	if (px == undefined || px == null || px == "") {
		px = "40px";
	}
	if (top == "" || top == '-90000px') {
		$(divId).style.top = px;
	} else {
		$(divId).style.top = "-90000px";
	}
	$(divId).style.zIndex=10;

	if ($F("email")) {
		$("password").focus();
	}
}

function fadeOutAll(inout,m,ids){
	str="";
	b="";
	for (i = 0; i < ids.length - 1; i++) {
		str=str+"jQuery('#"+ids[i]+"')."+inout+"("+m+",function(){";
		if (i > 0)
			b=b+";})";
	}
	str=str+"jQuery('#"+ids[ids.length - 1]+"')."+inout+"("+m+");})"+b;
	eval(str);	
}

// 以下代码为注册验证，尚未完善
function userNameCheck(){
	var userName=document.getElementById("userName");
	
	var name=userName.value;
	var checkStyleOne=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	var checkStyleTwo=/^[^!]{6,20}$/;
	var tips="";
	if(name==""){
		tips="请输入用户名";
	}else if(!checkStyleTwo.test(name)){
		tips="长度为6-20个字符";
	}else if(!checkStyleOne.test(name)){
		tips="邮箱格式不对";
	}
	
	if(tips!=""){
		jQuery("big#userNameCheck").replaceWith("<big id='userNameCheck'>"+tips+"</big>");
		userName.focus();
		return;
	}
	
	jQuery.ajax({
		   type: "GET",
		   url: "AjaxServer",
		   data: "type=validate&userName="+name,
		   beforeSend:function(){
				jQuery("big#userNameCheck").replaceWith("<big id='userNameCheck'>加载...<input type='image' src='images/loadings.gif'></big>");
	       },
		   success: function(msg){
	    	  var tips="";
	    	  if(parseInt(msg)==1){
	    		  tips="<input type='image' src='images/icon_ok.jpg'>";
	    	  }else{
	    		  tips="用户名已经存在";
	    	  }
	    	  jQuery("big#userNameCheck").replaceWith("<big id='userNameCheck'>"+tips+"</big>");
	      }
		});
}

function passwordCheck(){
	var passwords=document.getElementById("passwords");
	var pass=passwords.value;
	var checkStyleTwo=/^(\w){6,20}$/;
	var tips="<input type='image' src='images/icon_ok.jpg'>";
	if(pass==""){
		tips="请输入密码";
	}
	if(!checkStyleTwo.test(pass)){
		tips="只能输入6-20个字母、数字、下划线";
	}
	jQuery("big#passwordCheck").replaceWith("<big id='passwordCheck'>"+tips+"</big>");
	passwords.focus();
	return;
}

function rPasswordCheck(){
	var rPass=document.getElementById("rPassword");
	var pass=document.getElementById("passwords");
	var tips="<input type='image' src='images/icon_ok.jpg'>";
	if(rPass.value==""){
		tips="请确认密码";
	}else if(pass.value!=rPass.value)	{
		tips="密码不一致";
	}
	jQuery("big#rPasswordCheck").replaceWith("<big id='rPasswordCheck'>"+tips+"</big>");
	rPass.focus();
	return;
}

var URL;
function Load(url){
	URL =url; 

	for(i=5;i>=0;i--){ 
		window.setTimeout('doUpdate(' + i + ')', (5-i) * 1000);
	} 
} 
function doUpdate(num){ 
	document.getElementById('ShowDiv').innerHTML = '将在'+num+'秒后自动跳转到上一个页 面  ' ; 
	if(num == 0) { window.location=URL; } 
} 

function login(page) {
	// form.submit();
	showHideLoginDiv("loginDiv");
	param = "email=" + $F("email") + "&password=" + $F("password");
	var myAjax = new Ajax.Request(page+".top.login", {
		method :'post',
		parameters :param,
		onComplete :updateLoginInfo
	});
}

function logout(page) {
	var myAjax = new Ajax.Request(page + ".top.logout", {
		method :'post',
		parameters :null,
		onComplete :updateLoginInfo
	});
}

function updateLoginInfo(originalRequest) {
	var response = originalRequest.responseText;
	if (response == "login") {
		$("login").innerHTML = $("logined").innerHTML;
	} else if (response == "loginFalse") {
		alert("密码或用户名不正确！");
	} else if (response == "logout") {
		$("login").innerHTML = $("notLogin").innerHTML;
	}
}

function updateSub(originalRequest) {
	var response = originalRequest.responseText;
	var responseArr = response.split("@");
	var subName = responseArr[0].split(",");
	var subId = responseArr[1].split(",");

	for (i = 0; i <= subName.length-1; i++) {
		jQuery('#subType').append(
				'<option value="' + subId[i] + '">' + subName[i] + '</option>');
	}
}
